package com.shamine.kuaiqiao.blink.tool

import java.security.MessageDigest
import java.security.NoSuchAlgorithmException
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * 文字检查转换
 * Created by caine on 11/11/17.
 */
open class TextTool {
    /**
     * 验证邮箱
     */
    fun regexEmail(email: String): Boolean{
        val str = "^[A-Za-z0-9\\\\u4e00-\\\\u9fa5]+@[a-zA-Z0-9_-]+(\\\\.[a-zA-Z0-9_-]+)+\$"
        val pattern: Pattern = Pattern.compile(str)
        val matcher: Matcher = pattern.matcher(email)
        return matcher.matches()
    }
    /**
     * 验证密码
     */
    fun regexPassword(password: String): Boolean{
        val str = "^[a-zA-Z0-9]{6,15}\$"
        val pattern: Pattern = Pattern.compile(str)
        val matcher: Matcher = pattern.matcher(password)
        return matcher.matches()
    }

    /**
     * 验证电话号码
     */
    fun regexPhone(phone: String): Boolean{
        val str = "^1(3|4|5|7|8)[0-9]\\d{8}$"
        val pattern: Pattern = Pattern.compile(str)
        val matcher: Matcher = pattern.matcher(phone)
        return matcher.matches()
    }

    /**
     * 吧String字符串進行md5編碼返回
     */
    fun encryptPwd(pwd: String): String {
        try {
            val md5 = MessageDigest.getInstance("MD5")
            val bytes = md5.digest(pwd.toByteArray())
            var result = ""
            for (b in bytes) {
                var temp = Integer.toHexString(b.toInt() and 0xff)
                if (temp.length == 1) {
                    temp = "0" + temp
                }
                result += temp
            }
            return result
        } catch (e: NoSuchAlgorithmException) {
            e.printStackTrace()
        }

        return ""
    }
}